<?php

require_once 'ActionFramework/start.php';
require_once 'actions/continue_signup.cls.php';

require_once 'util.php';

class SignupAction extends Action {
	
	const INPUT_NAME = "name";
	const INPUT_EMAIL = "email";
	
	public function __construct() {
		parent::__construct();
		$this->add_input(new TextInput(SignupAction::INPUT_NAME, 'Name', $min_length=2, $max_length=50));
		$this->add_input(new EmailInput(SignupAction::INPUT_EMAIL, 'E-mail'));
	}
	
	public function trigger($request) {
		$name = $request[SignupAction::INPUT_NAME];
		$email = $request[SignupAction::INPUT_EMAIL];
		
		global $DB;
		if ($DB->is_email_known($email)) {
			return r(new StartAction("Registration failed, because this email address is already in use."))->trigger($request);
		}
		$secret = $DB->create_new_signup_user($name, $email);
		
		$action = new ContinueSignupAction();
		$link = $action->get_direct_link();
		$link->set_value(ContinueSignupAction::INPUT_SECRET, $secret);
		$url = current_page_url() . $link->__toString();
		
		LT_mail($email, 'Tandem Delft website registration', "

Hi $name,

Someone - we assume you - just registered on the Tandem Delft website. You can finish the
registration process by following this link: $url

Kind regards,

The Language Exchange team

		");
		
		$message = "<p>An email has been send to $email. Click the link in the email to validate your email address 
						and complete registration.</p>";
		
		return new MainView(null, $message);	
	}
	
}
